﻿/*+===================================================================
File:      BulletItem.cpp

Summary:   Định nghĩa các phương thức của CBulletItem.
===================================================================+*/

#include "BulletItem.h"


CBulletItem::CBulletItem(D3DXVECTOR2 pos, int id, int typeID) : CMoveableObject(pos, id, typeID)
{
	CResourcesManager::GetInstance()->GetSprite(ID_SPRITE_M_ITEM);
	CResourcesManager::GetInstance()->GetSprite(ID_SPRITE_B_ITEM);
	CResourcesManager::GetInstance()->GetSprite(ID_SPRITE_F_ITEM);
	CResourcesManager::GetInstance()->GetSprite(ID_SPRITE_S_ITEM);
	CResourcesManager::GetInstance()->GetSprite(ID_SPRITE_L_ITEM);
	CResourcesManager::GetInstance()->GetSprite(ID_SPRITE_R_ITEM);
	CResourcesManager::GetInstance()->GetSprite(ID_SPRITE_EAGLE_ITEM);
	switch (typeID)
	{
	case ID_M_ITEM:
		m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_M_ITEM);
		break;
	case ID_B_ITEM:
		m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_B_ITEM);
		break;
	case ID_F_ITEM:
		m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_F_ITEM);
		break;
	case ID_S_ITEM:
		m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_S_ITEM);
		break;
	case ID_L_ITEM:
		m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_L_ITEM);
		break;
	case ID_R_ITEM:
		m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_R_ITEM);
		break;
	case ID_EAGLE_ITEM:
		m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_EAGLE_ITEM);
		break;
	}

	switch (g_nCurentLvl)
	{
	case ID_LEVEL1:
	case ID_LEVEL3:
		m_v.x = 70.0f;
		m_v.y = 350.0f;
		break;
	case ID_LEVEL2:
		m_v.x = -10.0f;
		m_v.y = 400.0f;
	}

	m_a.y = GRAVITY;
	m_box.width = 48;
	m_box.height = 30;

	m_bIsLive = true;
}

CBulletItem::~CBulletItem()
{

}

void CBulletItem::Update(float timeFrame)
{
	if (!m_bIsLive)
		return;

	m_pSprite->Update(timeFrame);
	UpdateBox(timeFrame);
}

void CBulletItem::UpdateCollision(vector<CGameObject*> vecGameObjs, float timeFrame)
{
	if (!m_bIsLive)
		return;

	float timeCollision = timeFrame;
	float normalx = 0.0f, normaly = 0.0f;

	for (int i = 0; i < vecGameObjs.size(); i++)
	{
		if (vecGameObjs[i]->IsLive())
		{
			float timeCollision = CheckCollision(vecGameObjs[i], normalx, normaly, timeFrame);
			if (timeCollision < timeFrame) //Xảy ra va chạm
			{
				switch (vecGameObjs[i]->GetTypeId())
				{
				case ID_GRASS:
				case ID_GRASS_CAN_FALL:
					if (timeCollision < timeFrame)
						if (normaly == 1)
						{
						m_pos += m_v * timeCollision;
						m_v.x = 0.0f;
						m_v.y = 0.0f;
						m_pos.y += 0.1f;
						}
					break;
				}
			}
		}
	}

	m_pos += m_v * timeFrame;
	m_v += m_a * timeFrame;
}